package com.htkj.demo

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.reflect.TypeToken
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.htkj.demo.bean.DeviceData
import com.htkj.demo.bean.PageInfo
import com.htkj.demo.dialog.SettingDialog
import com.htkj.demo.okhttp.OkHttpUtil
import com.htkj.demo.okhttp.ResponseListener
import com.htkj.demo.okhttp.ResultCallback
import com.lxj.xpopup.XPopup
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import org.json.JSONObject


class MainActivity : AppCompatActivity() {
    lateinit var  mEmptyControlVideo:EmptyControlVideo
    var mWebSocketManager:WebSocketManager?=null
    private var list:MutableList<PageInfo>?=null
    private var positing:Int=0;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true) //导航栏图标是深色，不写默认为亮色;
            .barEnable(true)
            .fullScreen(true)
            .hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
            .init()
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        //        // 所有子类都将继承这些相同的属性,请在设置界面之后设置
        requestedOrientation= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        setContentView(R.layout.activity_main)
        mEmptyControlVideo= findViewById<EmptyControlVideo>(R.id.mEmptyControlVideo)
        val mViewDebug=findViewById<View>(R.id.v_debug)
        mViewDebug.setOnLongClickListener {
//            val debug=CacheUtil.getString("DEBUG","0")
//            CacheUtil.setString("DEBUG",if(debug=="1") "0" else "1")
//            initWebView();
            XPopup.Builder(this)
                .asCustom(SettingDialog(this))
                .show()
            true
        }

        initWebView();
    }

    fun initWebView(){

        val IPS=CacheUtil.getString("IP","192.168.0.202")
        val MAC=CacheUtil.getString("MAC","00:00:00:00:01")
        val DEBUG=CacheUtil.getString("DEBUG","0")
        mWebSocketManager?.removeAllListeners()
        mWebSocketManager?.clear()
        mWebSocketManager=WebSocketManager.Builder().url("ws://${IPS}:8183/imserver/${MAC}").build()
        mWebSocketManager?.connect()

        mWebSocketManager?.addListener(object: MyWebSocketListener() {
            override fun onMessage(webSocket: WebSocket, text: String) {
                super.onMessage(webSocket, text)
                val json=JSONObject(text)
                json.getInt("type").apply {
                    when(this){
                        1->{
                            if (mEmptyControlVideo.getCurrentState()==EmptyControlVideo.CURRENT_STATE_PAUSE)  {
                                mEmptyControlVideo.onVideoResume();
                            }
                        }
                        2->{
                            if (mEmptyControlVideo.isInPlayingState)  {
                                // 当前正在播放，执行暂停
                                mEmptyControlVideo.onVideoPause();
                            }
                        }
                        3->{
                            mEmptyControlVideo.addVoluem()
                        }
                        4->{
                            mEmptyControlVideo.reduceVoluem()
                        }
                        5->{
                            if(positing>0){
                                positing--
                                initPayle()
                            }
                        }
                        6->{
                            if(list!=null&&list!!.size>positing+1){
                                positing++
                                initPayle()
                            }
                        }
                        7->{
                            positing=0;
                            initPayle()
                        }
                    }
                }

            }

            override fun onTocat(message: String) {
                Toast.makeText(this@MainActivity,message,0).show()
            }
        })

        OkHttpUtil.get("http://${IPS}:8183/pad/api/getByMac?mac=${MAC}",ResultCallback(object :ResponseListener<DeviceData>{
            override fun onSuccess(obj: DeviceData) {
                Log.e("OkHttpUtil",obj.toString())
                this@MainActivity.list=obj.pageList
                initPayle()
            }

            override fun onFailure(error: Any?) {

            }

        },object : TypeToken<DeviceData>() {}));
//        mWebView.loadUrl("file:///android_asset/index.html?IP=${IPS}&MAC=${MAC}&DEBUG=${DEBUG}");
    }


    fun initPayle(){
        list?.let {
            if(positing>=it.size){
                return
            }
            Log.e("httpurl",it[positing].url)
            Log.e("httpurl1",it[positing].url1)
            Log.e("httpurl1","positing=${positing}")
            mEmptyControlVideo.loadCoverImageBy(it[positing].url1)
            if(positing==0){
                mEmptyControlVideo.setUp(it[positing].url,true,null)
            }else{
                mEmptyControlVideo.setUp("",true,null)
            }
            mEmptyControlVideo.isLooping=true
            mEmptyControlVideo.startPlayLogic()

        }

    }


    override fun onDestroy() {
        super.onDestroy()
        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }


    override fun onResume() {
        super.onResume()
        mEmptyControlVideo.onVideoResume();
    }

    override fun onPause() {
        super.onPause()
        mEmptyControlVideo.onVideoPause()
    }
}


